import 'package:fish_redux/fish_redux.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hayll_app_village_master/hayll/network/yll_network.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/village_grid_component/bean/village_grid_response.dart';
import 'package:hayll_app_village_master/hayll/village_manager/network/village_action.dart';
import 'action.dart';
import 'state.dart';

Effect<VillageHousePickerState> buildEffect() {
  return combineEffects(<Object, Effect<VillageHousePickerState>>{
    VillageHousePickerAction.action: _onAction,
    VillageHousePickerAction.picker: _picker,
    Lifecycle.initState: _requestVillageHouseGrid,
  });
}

void _onAction(Action action, Context<VillageHousePickerState> ctx) {}

void _requestVillageHouseGrid(
    Action action, Context<VillageHousePickerState> ctx) {
  var params = {'villageId': 118};
  YllNetwork.requestPost(VillageAction.getVillageGrid, params: params,
      success: (json) {
    ctx.state.villageGrid = VillageGridResult.fromJson(json);
    if (ctx.state.villageGrid.buildingList == null) {
      ctx.state.villageGrid.buildingList = List();
    }
    ctx.state.villageGrid.buildingList.forEach((build) {
      if (build.itemList == null) {
        build.itemList = List();
      }
      build.itemList.forEach((build) {
        if (build.roomList == null) {
          build.roomList = List();
        }
        var allItem = VillageHouseGridResult();
        allItem.roomCode = '-1';
        allItem.itemId = -1;
        allItem.roomName = '整单元';
        build.roomList.insert(0, allItem);
      });
      var allItem = VillageUnitGridResult();
      allItem.itemCode = '-1';
      allItem.itemId = -1;
      allItem.itemName = '整栋楼';
      allItem.roomList = List();
      build.itemList.insert(0, allItem);
    });
    var allItem = VillageBuildGridResult();
    allItem.buildingCode = '-1';
    allItem.buildingId = -1;
    allItem.buildingName = '全小区';
    allItem.itemList = List();
    ctx.state.villageGrid.buildingList.insert(0, allItem);
    ctx.state.selectedBuildInfo = null;
    ctx.state.selectedUnitInfo = null;
    ctx.state.selectedHouseInfo = null;

    filterDatas(action, ctx, ctx.state.mode);
    // ctx.dispatch(VillageHousePickerActionCreator.refreshUI());
  }, failed: (errorCode, errorMsg) {
    Fluttertoast.showToast(msg: '$errorCode,$errorMsg');
  });
}

void _picker(Action action, Context<VillageHousePickerState> ctx) {
  var mode = action.payload['mode'];
  var data = action.payload['data'];
  if (data == null) {
    ctx.state.mode = mode;
    filterDatas(action, ctx, mode);
  } else {
    //修改模式
    if (mode == HousePickerMode.BUILD) {
      ctx.state.selectedBuildInfo = data;
      ctx.state.selectedUnitInfo = null;
      ctx.state.selectedHouseInfo = null;
      ctx.state.mode = ctx.state.selectedBuildInfo.buildingId == -1
          ? HousePickerMode.NONE
          : HousePickerMode.UNIT;
    } else if (mode == HousePickerMode.UNIT) {
      ctx.state.selectedUnitInfo = data;
      ctx.state.selectedHouseInfo = null;
      ctx.state.mode = ctx.state.selectedUnitInfo.itemId == -1
          ? HousePickerMode.NONE
          : HousePickerMode.HOUSE;
    } else {
      ctx.state.selectedHouseInfo = data;
      ctx.state.mode = HousePickerMode.NONE;
    }
    filterDatas(action, ctx, ctx.state.mode);
  }
}

void filterDatas(
    Action action, Context<VillageHousePickerState> ctx, HousePickerMode mode) {
  if (mode == HousePickerMode.BUILD) {
    ctx.state.datas.clear();
    ctx.state.datas.addAll(ctx.state.villageGrid.buildingList);
    ctx.dispatch(VillageHousePickerActionCreator.refreshUI());
  } else if (mode == HousePickerMode.UNIT) {
    ctx.state.datas.clear();
    ctx.state.datas.addAll(ctx.state.selectedBuildInfo.itemList);
    ctx.dispatch(VillageHousePickerActionCreator.refreshUI());
  } else if (mode == HousePickerMode.HOUSE) {
    ctx.state.datas.clear();
    ctx.state.datas.addAll(ctx.state.selectedUnitInfo.roomList);
    ctx.dispatch(VillageHousePickerActionCreator.refreshUI());
  } else {
//house
//判断是否选择了楼栋
    ctx.state.datas.clear();
    ctx.dispatch(VillageHousePickerActionCreator.refreshUI());
  }
}
